package com.xncoding.pos.utils;


import org.springframework.stereotype.Component;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

@Component
public class DateUtils {



   /* Instant：时间戳
    Duration：持续时间，时间差
    LocalDate：只包含日期，比如：2016-10-20
    LocalTime：只包含时间，比如：23:12:10
    LocalDateTime：包含日期和时间，比如：2016-10-20 23:14:21
    Period：时间段
    ZoneOffset：时区偏移量，比如：+8:00
    ZonedDateTime：带时区的时间
    Clock：时钟，比如获取目前美国纽约的时间
    */

    /**
     * 格式化模板
     **/
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 格式化成日期
     */
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");


    private static final String STR_DATE_FORMATTER = "yyyy-MM-dd";

    private static final String STR_DATE_TIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";


    /**
     * 设置日期时区常量
     **/
    public static final ZoneId CHINA_ZONE_ID = ZoneId.systemDefault();

    /**
     * 获取当前日期
     *
     * @return
     */
    public LocalDate getNowDate() {
        // 获取当前的日期时间
        LocalDateTime currentTime = LocalDateTime.now();
        LocalDate date1 = currentTime.toLocalDate();
        System.out.println("date1: " + date1);
        return date1;
    }

    /**
     * 获取当前日期字符串
     *
     * @return
     */
    public String getStrNowDate() {
        // 获取当前的日期时间
        LocalDateTime currentTime = LocalDateTime.now();
        LocalDate date1 = currentTime.toLocalDate();
        String format = date1.format(DATE_FORMATTER);
        System.out.println("date1: " + format);
        return format;
    }

    /**
     * 获取当前时间字符串
     *
     * @return
     */
    public String getStrNowTime() {
        LocalDateTime currentTime = LocalDateTime.now();
        String format = currentTime.format(DATE_TIME_FORMATTER);
        System.out.println("当前时间: " + format);
        return format;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public LocalDateTime getNowTime() {
        // 获取当前的日期时间
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println("当前时间: " + currentTime);

//        Date date = new Date();
//        LocalDateTime dateTime = date.toInstant().atZone(CHINA_ZONE_ID).toLocalDateTime();
//        System.out.println(dateTime);
        return currentTime;
    }

    /**
     * Date转为LocalDateTime (注:带有时间部分)
     **/
    public LocalDateTime toLocalDateTime(Date date) {
        LocalDateTime localDateTime2 = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        System.out.println(localDateTime2);
        return localDateTime2;
    }

    /**
     * LocalDateTime转Date
     **/
    public Date LocalDateTimeToDate(LocalDateTime localDateTime) {
        Instant instant3 = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        Date date3 = Date.from(instant3);
        return date3;
    }


    /**
     * LocalDate转Date
     **/
    public Date LocalDateToDate(LocalDate localDate) {
        //因为LocalDate不包含时间，所以转Date时，会默认转为当天的起始时间，00:00:00
        Instant instant4 = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        Date date4 = Date.from(instant4);
        return date4;
    }

    /**
     * Date与Instant的相互转化
     */
    public void toDate() {
        //Date与Instant的相互转化
        Instant instant = Instant.now();
        Date date = Date.from(instant);
        Instant instant2 = date.toInstant();
    }


    /**
     * 比较日期
     *
     * @param date1
     * @param date2
     * @return
     */
    public boolean compData(LocalDate date1, LocalDate date2) {
        return date1.isAfter(date2);
    }

    /**
     * 日期格式化(转为 2019-05-06 12:30:00)
     *
     * @param localDateTime
     * @return
     */
    public String dateTimeToStr(LocalDateTime localDateTime) {
        return localDateTime.format(DATE_TIME_FORMATTER);
    }

    /**
     * 日期格式化(转为 2019-05-06)
     *
     * @param localDate
     * @return
     */
    public String dateToStr(LocalDate localDate) {
        return localDate.format(DATE_FORMATTER);
    }


    /**
     * 日期格式化(2019-08-10)
     *
     * @param data
     * @return
     */
    public LocalDate StrToLocalDate(String data) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(STR_DATE_FORMATTER);
        LocalDate formatted = LocalDate.parse(data, formatter);
        System.out.println(formatted);
        return formatted;
    }


    /**
     * 日期格式化(2019-08-10 12:30:00)
     *
     * @param data
     * @return
     */
    public LocalDateTime StrToLocalDateTime(String data) {
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(STR_DATE_TIME_FORMATTER);
        LocalDateTime formatted = LocalDateTime.parse(data, formatter2);
        System.out.println(formatted);
        return formatted;
    }

    /**
     * 计算两个日期的差值
     *
     * @param date1
     * @param date2
     * @return
     */
    public Long dataDiff(Date date1, Date date2) {
        Instant fromDate = date1.toInstant();
        Instant toDate = date2.toInstant();
//        long minutes = ChronoUnit.MINUTES.between(fromDate, toDate);//分钟
        long hours = ChronoUnit.HOURS.between(fromDate, toDate);//小时
        return hours;
    }

    /**
     * 计算日期差值
     *
     * @param date1
     * @param date2
     * @return
     */
    public Long dataDiffer(Date date1, Date date2) {
        Instant inst1 = Instant.now();  //当前的时间
        System.out.println("Inst1：" + inst1);
        Instant inst2 = inst1.plus(Duration.ofSeconds(10));     //当前时间+10秒后的时间
        System.out.println("Inst2：" + inst2);
        Instant inst3 = inst1.plus(Duration.ofDays(125));       //当前时间+125天后的时间
        System.out.println("inst3：" + inst3);

        Duration between = Duration.between(inst1, inst3);
        System.out.println("以毫秒计的时间差：" + Duration.between(inst1, inst2).toMillis());

        System.out.println("以秒计的时间差：" + Duration.between(inst1, inst3).getSeconds());

        return between.toMinutes();
    }

    /**
     * 日期时间间隔
     **/
    public void betweenDay() {
        // LocalDateTime
        LocalDateTime startDate = LocalDateTime.of(2019, 07, 01, 12, 12, 22);
        LocalDateTime endDate = LocalDateTime.of(2019, 07, 03, 12, 12, 22);
        Long withSecond = endDate.atZone(CHINA_ZONE_ID).toEpochSecond() - startDate.atZone(CHINA_ZONE_ID).toEpochSecond();
        System.out.println(withSecond / 60 / 60 / 24);

        // LocalDate
        LocalDate startDate2 = LocalDate.of(2019, 07, 01);
        LocalDate endDate2 = LocalDate.of(2019, 07, 03);
        Long withSecond2 = endDate2.toEpochDay() - startDate2.toEpochDay();
        System.out.println(withSecond2);
    }


    /**
     * 日期加减
     **/
    public void plusDay() {
        LocalDateTime dateTime = LocalDateTime.now(CHINA_ZONE_ID);
        //天
        dateTime = dateTime.plusDays(1);
        //时
        dateTime = dateTime.plusHours(-1);
        //分钟
        dateTime = dateTime.plusMinutes(30);
        System.out.println(dateTime.format(DATE_TIME_FORMATTER));
    }


    /**
     * 第一天and最后一天
     **/
    public void theLastDay() {
        // 当月第一天
        LocalDateTime dateTime = LocalDateTime.of(2019, 07, 03, 12, 12, 22);
        dateTime = dateTime.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(dateTime);
        // 当月最后一天
        dateTime = dateTime.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(dateTime);

        //当月的第几天
        dateTime = LocalDateTime.now();
        int dayOfMonth = dateTime.getDayOfMonth();
        System.out.println(dayOfMonth);
        // 当前周的第几天
        int dayOfWeek = dateTime.getDayOfWeek().getValue();
        System.out.println(dayOfWeek);
    }


    /**
     * 获取年月日信息
     **/
    public void getYearAndMonth() {
        LocalDate date = LocalDate.now();
        System.out.printf("年=%d， 月=%d， 日=%d", date.getYear(), date.getMonthValue(), date.getDayOfMonth());
    }

    public static void main(String[] args) {
//        LocalDate oneday = LocalDate.now();


//判断是否为闰年
//        boolean isLeapYear = oneday.isLeapYear();
//        System.out.println(isLeapYear);


        DateUtils dateUtils = new DateUtils();
//        dateUtils.getNowDate();

        // 解析字符串
//        LocalTime date5 = LocalTime.parse("20:15:30");
//        System.out.println("date5: " + date5);

//        dateUtils.getStrNowDate();

        //构造日期
//        LocalDate date = LocalDate.of(2000, 1, 1);
//        System.out.println("千禧年=" + date);


        dateUtils.toLocalDateTime(new Date());

    }
}
